/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2014  Georg Rudoy
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************/

#include "recursivedirwatcher_mac.h"
#include <QtDebug>
#include <Cocoa/Cocoa.h>
#include <CoreServices/CoreServices.h>
#include <CoreFoundation/CoreFoundation.h>
#include <CoreFoundation/CFArray.h>

namespace LeechCraft
{
namespace Plugin::SMP
{
	RecursiveDirWatcherImpl::RecursiveDirWatcherImpl (QObject *parent)
	: QObject { parent }
	{
	}

	RecursiveDirWatcherImpl::~RecursiveDirWatcherImpl ()
	{
		Stop ();
	}

	void RecursiveDirWatcherImpl::AddRoot (const QString& root)
	{
		if (Dirs_.contains (root))
			return;

		Dirs_ << root;
		Stop ();
		Start ();
	}

	void RecursiveDirWatcherImpl::RemoveRoot (const QString& root)
	{
		if (Dirs_.removeOne (root))
		{
			Stop ();
			Start ();
		}
	}

	void RecursiveDirWatcherImpl::Notify (quint64 eventId, const QString& path)
	{
		MaxEventId_ = std::max (MaxEventId_, eventId);
		emit directoryChanged (path);
	}

	bool RecursiveDirWatcherImpl::IsRunning () const
	{
		return EvStream_;
	}

	namespace
	{
		void fsEventsCb (ConstFSEventStreamRef, void *clientCallBackInfo,
				size_t numEvents, void *eventPaths,
				const FSEventStreamEventFlags eventFlags [], const FSEventStreamEventId eventIds [])
		{
			auto impl = static_cast<RecursiveDirWatcherImpl*> (clientCallBackInfo);

			auto paths = static_cast<char**> (eventPaths);

			for (size_t i = 0; i < numEvents; ++i)
			{
				if (eventFlags [i] & kFSEventStreamEventFlagHistoryDone)
					continue;

				impl->Notify (eventIds [i], QString::fromUtf8 (paths [i]));
			}
		}
	}

	void RecursiveDirWatcherImpl::Start ()
	{
		if (IsRunning () || Dirs_.isEmpty ())
			return;

		auto pathsToWatch = [[NSMutableArray alloc] init];
		for (const auto& dir : Dirs_)
		{
			auto path = [[NSString alloc] initWithUTF8String: dir.toUtf8 ().constData ()];
			[pathsToWatch addObject: path];
			[path release];
		}

		FSEventStreamContext ctx { 0, this, nullptr, nullptr, nullptr };
		const quint64 sinceId = MaxEventId_ ? MaxEventId_ : kFSEventStreamEventIdSinceNow;

		EvStream_ = FSEventStreamCreate (kCFAllocatorDefault,
				&fsEventsCb, &ctx, reinterpret_cast<CFArrayRef> (pathsToWatch),
				sinceId, 2, kFSEventStreamCreateFlagNone);

		FSEventStreamScheduleWithRunLoop (EvStream_,
				CFRunLoopGetCurrent (), kCFRunLoopDefaultMode);

		if (!FSEventStreamStart (EvStream_))
			qWarning () << Q_FUNC_INFO
					<< "cannot start event stream";

		[pathsToWatch release];
	}

	void RecursiveDirWatcherImpl::Stop ()
	{
		if (!IsRunning ())
			return;

		FSEventStreamStop (EvStream_);
		FSEventStreamUnscheduleFromRunLoop (EvStream_,
				CFRunLoopGetCurrent (), kCFRunLoopDefaultMode);
		FSEventStreamInvalidate (EvStream_);
		FSEventStreamRelease (EvStream_);

		EvStream_ = nullptr;
	}
}
}
